<!DOCTYPE html>
<html>
<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <link rel="stylesheet" href="/js/eleditor/layout/base.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/css/dialog.css"/>
    <script src="/js/plugins/dialog2/js/dialog.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/plugins/My97DatePicker/lang/zh-cn.js"></script>
    <script src="/js/eleditor/Eleditor.min.js"></script>
    <script src="/js/eleditor/webuploader.min.js"></script>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            //查询地区-----------------------------
            $.get("/region", function (data) {
                var html = "";
                $.each(data, function (index, item) {
                    html += "<option value="+ item.id + ">" + item.name + "</option>";
                })
                $("#place").html(html);
                //获取游记对应的id
                var params = getParams();
                if (params.id) {
                    $.get("/travels/"+params.id, function (data) {
                        //回显操作
                        $(".travels").renderValues(data);
                        //编辑操作
                        $("input[name='_method']").val("PUT");
                        $("#detailForm").attr("action", "/travels/" + params.id);
                    })
                }
                //end--------------------------------
            })
            //更换封面----------------------------
            //点击更换封面时,弹出选择文件框
            $("#changeBtn").click(function () {
                $("#file").click();
            })
            //当封面内容发生改变
            $("#file").change(function () {
                var value = $(this).val();
                //判断选择框是否有值
                if (value) {
                    //获取图片
                    $("#coverForm").ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            //图片回显
                            $("#coverUrl").attr("src", data.url);
                        }
                    })
                }
            })
            //end--------------------------------+
            //富文本编辑框
            var Edr = new Eleditor({
                //富文本框编辑开始位置
                el: '#contentEditor',
                //图片上传路径
                upload: {
                    server: '/images',
                },
            });
            //end--------------------------------
            //游记的保存操作
            //发布/保存
            //当点击保存按钮
            $("#save").click(function () {
                var state = $(this).data("state")
                //提交表单
                $("#detailForm").ajaxSubmit({
                    //存在问题: 游记保存时,表单中没有四个参数,需要额外添加
                    beforeSubmit: function (arr, form, options) {
                        //arr表示表单中每一个控件包装对象数组
                        arr.push({name: "title", value: $("#title").val()});
                        arr.push({name: "coverUrl", value: $("#coverUrl").attr("src")});
                        arr.push({name: "state", value: state});
                        arr.push({name: "travelContent.content", value: Edr.getContent()});
                    },
                    //表单提交后执行
                    success: function (data) {
                        if (data.success) {
                            //跳转页面
                            window.location.href = "/mine/travelContent.html";
                        }
                    }
                })
            })
        })
    </script>
</head>
<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save" data-state="0"><span>保存</span></i>
            <i class="fa fa-check" id="release" data-state="1"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>
        <form id="coverForm" method="post" action="/images" enctype="multipart/form-data">
            <input type="file" name="file" id="file" style="display: none"
                   accept="image/gif,image/jpeg,image/jpg,image/png"/>
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
        </form>

    </div>
    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" render-value="title"/>
    </div>
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post" action="/travels">
            <input type="hidden" name="id" render-value="id"/>
            <input type="hidden" name="_method" />
            <label>出行时间:</label>
            <input type="text" name="travelTime" class="form-control" onclick="WdatePicker()"
                   render-value="travelTime"/>
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="place" name="place.id" render-value="place.id">
            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" name="isPublic" render-attr="checked=isPublic">是否公开
                </label>
            </div>
        </form>
    </div>

    <hr/>

</div>


</body>

</html>